今天來試試看實作範例囉~
├── Dockerfile
├── docker-compose.yml
├── k8s.yaml
├── main.py
├── requirements.txt
└── utils
├── app.py
├── data.py
前置作業~要先在本地端有docker image 可以使用喔
FROM python:3.8.2
RUN mkdir onlineweb
WORKDIR onlineweb
COPY . ./
RUN apt-get update -y && apt-get install cron -y \
&& apt-get install vim -y
# 下載套件
RUN pip install -r requirements.txt
# 設權限
RUN chmod -cR 700 *
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8001"]
version: '3'
services:
onlineweb:
restart: always
build: ./
image: onlineweb
container_name: onlineweb
ports:
- "8001:8001"
volumes: # connect at local file HOST:CONTAINER
- ./:/onlineweb
# connect network on api-cluster
networks:
default:
external:
name: prometheus_monitoring
docker build -t onlineweb .
docker images
完成前置作業,來放上minikube上!
這邊新手常見問題,調用本地的image 會出現錯誤ErrImageNeverPull
參考文件一或是參考文件二
docker login
ps. {"credsStore": "desktop"} 不會看到auth的項目,而是以credsStore存儲名稱
將憑證放進k8s中
kubectl create secret generic regcred \
--from-file=.dockerconfigjson=<path/to/.docker/config.json> \
--type=kubernetes.io/dockerconfigjson
Kubernetes 使用在單節點集群內運行的 Docker 守護程序,而不是主機!eval $(minikube docker-env)
執行 kubectl apply -f k8s.yaml
## k8s.yaml
---
apiVersion: v1
kind: Service # 指定類型為 Service
metadata:
name: onlineweb-web #Service的名稱為 web
spec: #規格描述
selector:
app: onlineweb
ports:
- protocol: "TCP" # Service 支援TCP與UDP兩種protocl,預設為TCP
port: 8001
targetPort: 8001
type: LoadBalancer
--- #利用這個來區隔不同物件的設定
apiVersion: apps/v1
kind: Deployment # 指定類型為 Deployment
metadata:
name: onlineweb
spec:
selector:
matchLabels:
app: onlineweb
replicas: 3
template:
metadata:
labels:
app: onlineweb
spec:
containers:
- name: onlineweb
image: onlineweb:latest
imagePullPolicy: Never
ports:
- containerPort: 8001
部署上去後~
進去看看kubectl exec -ti PodName -- /bin/sh
打開onlineweb網站來看看
這邊為啥要有service呢?
kubectl port-forward onlineweb 8001:8001
補充 deployment & service的解釋,來自參考資料[1]
A deployment is responsible for keeping a set of pods running.
A service is responsible for enabling network access to a set of pods.
參考資料